package com.hhkj.admin.question.service.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hhkj.admin.question.common.cont.QuestionFormatConst;
import com.hhkj.admin.question.common.cont.QuestionImportConst;
import com.hhkj.admin.question.common.cont.QuestionTypeEnum;
import com.hhkj.admin.question.common.utils.QuestionImportTemplateUtils;
import com.hhkj.admin.question.common.utils.QuestionImportUtils;
import com.hhkj.admin.question.domain.Question;
import com.hhkj.admin.question.model.dopo.QuestionDo;
import com.hhkj.admin.question.model.dopo.QuestionPo;
import com.hhkj.admin.question.model.extend.QuestionImportModel;
import com.hhkj.admin.question.model.req.ExcelTypeCountReq;
import com.hhkj.admin.question.model.req.QuestionReq;
import com.hhkj.admin.question.model.res.ExcelTypeCountRes;
import com.hhkj.admin.question.model.res.QuestionRes;
import com.hhkj.admin.question.service.base.QuestionService;
import com.hhkj.admin.question.service.biz.QuestionBizService;
import com.hhkj.admin.question.service.extend.QuestionExtendService;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.model.ExcelCreateModel;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * QuestionBizServiceImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/8 15:59
 */
@Service
public class QuestionBizServiceImpl
        extends BaseBizServiceImpl<Question, QuestionReq, QuestionRes, QuestionDo, QuestionPo, QuestionService, QuestionExtendService>
        implements QuestionBizService
{


    @Override
    public Workbook createImportTemplate()
    {
        Workbook template = ExcelUtils.createExcelWorkbookSheet(new ExcelCreateModel(
                Question.class.getSimpleName(),
                QuestionImportModel.TITLE_NAME,
                new ArrayList<>(QuestionImportConst.QUESTION_DEMO_LIST),
                QuestionImportModel.class));
        QuestionImportTemplateUtils.supplySelectList(template);
        //设置表格格式
        QuestionImportTemplateUtils.formatHeader(template, SysConst.IMPORT_MODEL);
        return template;
    }

    @Override
    public ExcelTypeCountRes analysisImportExcel(ExcelTypeCountReq req)
    {
        List<QuestionReq> questionReqList = QuestionImportUtils.readExcelQuestionList(req);
        Map<String, List<QuestionReq>> questionListMap = questionReqList.stream()
                .collect(Collectors.groupingBy(QuestionReq::getTypeCode));
        ExcelTypeCountRes result = new ExcelTypeCountRes();
        Map<String, Integer> questionTypeAmountMap =
                questionReqList.stream()
                        .collect(Collectors.groupingBy(QuestionReq::getTypeCode,
                                Collectors.collectingAndThen(Collectors.toList(), List::size)));
        result.setQuestionTypeAmountMap(questionTypeAmountMap);
        if (questionListMap.containsKey(QuestionTypeEnum.CASE.getCode())) {
            List<QuestionReq> caseQuestionList = questionListMap.get(QuestionTypeEnum.CASE.getCode());
            QuestionReq firstQuestion = CollectionUtil.getFirst(caseQuestionList);
            if (CollectionUtil.isNotEmpty(firstQuestion.getQuestionList())) {

                Map<String, Integer> caseQuestionTypeAmountMap = firstQuestion.getQuestionList().stream()
                        .collect(Collectors.groupingBy(QuestionReq::getTypeCode,
                                Collectors.collectingAndThen(Collectors.toList(), List::size)));

                int caseGroupSize = firstQuestion.getQuestionList().size();
                result.setCaseGroupSize(caseGroupSize);
                result.setCaseQuestionTypeAmountMap(caseQuestionTypeAmountMap);
            }
        }
        return result;
    }

    @Override
    protected void beforeUpdate(QuestionReq req)
    {
        Question questionPo = this.getService().getById(req.getId());
        if (ObjectUtil.isNull(questionPo)) {
            throw new GlobalException("题目信息不存在");
        }
        if (!QuestionTypeEnum.CASE.getCode().equals(questionPo.getTypeCode())) {
            if (StrUtil.isBlank(req.getAnswer())) {
                throw new GlobalException("答案不能为空");
            } else if (StrUtil.isNotBlank(req.getAnswer())) {
                // 去掉答案的所有空行
                String answerContent = req.getAnswer();
                if (QuestionTypeEnum.MULTIPLE.getCode().equals(questionPo.getTypeCode())) {
                    req.setAnswer(answerContent.replaceAll("\\s*", ""));
                } else {
                    req.setAnswer(answerContent.trim());
                }
                // 答案规则校验
                if (QuestionTypeEnum.SINGLE.getCode().equals(questionPo.getTypeCode())
                        || QuestionTypeEnum.MULTIPLE.getCode().equals(questionPo.getTypeCode())) {
                    if (!req.getAnswer().matches(QuestionFormatConst.CHOOSE_REGEX)) {
                        throw new GlobalException("答案格式错误");
                    }
                }
                if (QuestionTypeEnum.JUDGE.getCode().equals(questionPo.getTypeCode())) {
                    if (!req.getAnswer().matches(QuestionFormatConst.JUDGE_REGEX)) {
                        throw new GlobalException("答案格式错误");
                    }
                }
            }
        }
    }
}
